using System;
using System.Collections;
using System.Collections.Generic;
using PropertyExpression.TypedDataBinding.Web.Example.Exapmle1.Model;

namespace PropertyExpression.TypedDataBinding.Web.Example.Exapmle1.Model
{
    public class DataFactory
    {
        public static DataFactory Instance = new DataFactory();

        private DataFactory()
        {
            dictionary.Add(typeof (A),
                           new[]
                               {
                                   new A(1),
                                   new A(2),
                                   new A(3),
                               });
            dictionary.Add(typeof(B),
                           new[]
                               {
                                   new B(1, 1),
                                   new B(2, 1),
                                   new B(3, 1),
                               });
            dictionary.Add(typeof(C),
                           new[]
                               {
                                   new C(1, 1),
                                   new C(2, 1),
                                   new C(3, 1),
                                   new C(4, 3),
                                   new C(5, 3),
                               });
        }

        private readonly IDictionary<Type, IEnumerable> dictionary = new Dictionary<Type, IEnumerable>();

        public T GetById<T>(int id) where T : IdOwner
        {
            foreach (T a in GetAll<T>())
            {
                if (a.Id == id)
                {
                    return a;
                }
            }
            throw new ApplicationException();
        }

        public IEnumerable<T> GetAll<T>() where T : IdOwner
        {
            return (IEnumerable<T>) dictionary[typeof (T)];
        }
    }
}